#include "pro.h"

/** main function **/

int main(int argc, char* argv[]) {

    //Output help information.
    cout << "./scan distance coherentLength numberOfSample";
    cout << " samplingDistance numberReceivedSignal bDistance ";
    cout << " step numOfStep" << endl;

    if (argc<9) {
	cout << "Command ERROR... " << endl;
	return 0;
    } 

    //argv[1] distance5;
    cout << "    distance = " << argv[1] << endl;
    cout << "    coherentLength = " << argv[2] << endl;
    cout << "    numberOfSample = " << argv[3] << endl;
    cout << "    samplingDistance = " << argv[4] << endl;
    cout << "    numberReceivedSignal = " << argv[5] << endl;
    cout << "    bDistance = " << argv[6] << endl;
    cout << "    step = " << argv[7] << endl;
    cout << "    numOfStep = " << argv[8] << endl;

    /* The parameters of the first function.**/
    int imageLength=100;
    float* imageIntensity;
    float* imageLocation;
    imageIntensity=new float[imageLength];
    imageLocation=new float[imageLength];
    int k1;

    /* The parameters of the second function. */
    int coherentLength = atoi(argv[2])    /*Coherent length */;
    int n = atoi(argv[3]);    /* Number of samples in unit coherentLength */
    int numberOfSignal=n*coherentLength;/* Number of samples.*/
    float samplingDistance = atof(argv[4]);  /* Sampling distance. */
    float* signal;/* Signal*/
    signal = new float[imageLength*numberOfSignal];
	
    int k2;

    /* The parameter of third function. */
    int numberOfDetector = 11;      /*Number of detectors*/
    float distance = atoi(argv[1]); /* Distance between image and detectors.*/
    float* delay;
    float delocation[11]={-399,-320,-240,-160,-80,0,80,160,240,320,400};
    float* detectorLocation=delocation;
    delay = new float[imageLength*numberOfDetector];

    int k3;

    /* The parameters of the fourth function. */
    int numberOfReceivedSignal = atoi(argv[5]);
	
    float* receivedSignal;
    int i;
    receivedSignal = new float[numberOfDetector*numberOfReceivedSignal];
    for (i=0;i<numberOfDetector*numberOfReceivedSignal;i++) {
	*(receivedSignal+i) = 0;
    }
    int k4;

    /* The fifth function. */
    float distance5=atoi(argv[1]);
    float* image;
    float bDistance = atof(argv[6]);
    float step = atof(argv[7]);
    int numOfStep = atoi(argv[8]);

    image=new float[imageLength*numOfStep];
    int k5;

    /* Call functions. */
    k1=ImageIntensityDistribution(imageIntensity,
				  imageLocation, imageLength, false);
    k2=SourceSignalGeneration(signal, imageIntensity,
			      imageLocation, imageLength,
			      samplingDistance, coherentLength,
			      numberOfSignal, false);
    k3=DelayMatrix(delay, imageLocation, imageLength,
		   detectorLocation, numberOfDetector, distance, false);
    k4=RecievedSignalGeneration(receivedSignal, signal, delay,
				imageLength, numberOfDetector,
				samplingDistance, numberOfReceivedSignal,
				detectorLocation, numberOfSignal,
				distance, coherentLength, false);
    //Obtain a group of images.
    for (i=0;i<numOfStep;i++) {
	distance5 = bDistance + float(i)*step;
	cout << "   Reconstruct image at distance " << distance5 << endl;
	k3=DelayMatrix(delay, imageLocation, imageLength,
		       detectorLocation, numberOfDetector, 
		       distance5, false);
	k5=HolographicImaging(image+i*imageLength, imageLocation, imageLength,
			      receivedSignal, numberOfDetector,
			      numberOfReceivedSignal, detectorLocation,
			      samplingDistance, distance5,
			      delay, coherentLength, numberOfSignal, false);
    }
    //Output images to files.
    ofstream file5("ScanImageP.txt");
    int j;
    if (!file5) {
	cout << "File Open ERROR!";
	return 0;
    }
    for (j=0;j<numOfStep;j++) {
	for (i=0;i<imageLength;i++) {
	    file5 << image[j*imageLength+i];
	    file5 << " ";
	}
	file5 << endl;
    }
    file5.close();

    delete []imageLocation;
    delete []imageIntensity;
    delete []signal;
    delete []delay;
    delete []receivedSignal;
    delete []image;
    return 0;

}
